package game.block.trap;

import game.block.Locator;
import game.block.command.CancelCommand;
import game.block.command.Command;
import game.block.command.TeleportCommand;
import game.block.entity.MovableEntity;
import game.block.entity.TrapEntity;
import game.block.game.Board;
import game.block.tile.Tile;
import game.block.turn.Turn;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.g2d.ParticleEffect;
import com.badlogic.gdx.graphics.g2d.ParticleEmitter;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.math.Vector3;

public class Teleport extends TrapEntity {

    private Tile spawnTile;
    private int id;
    private ParticleEffect particles;
    private OrthographicCamera camera;

    public Teleport(Vector2 pos, Tile tile, int id, OrthographicCamera camera) {
        super(pos, tile, HOLE, TRAPTYPE.TELEPORT);
        spawnTile = null;
        this.camera=camera;
        this.id = id;
        particles = new ParticleEffect();
        particles.load(Gdx.files.internal("data/art/teleport"), Gdx.files.internal("data/art/"));
        Locator.getEffects().add(particles);
        Vector3 tPos = new Vector3(tile.getPosition().x + TILE_SIZE/2, tile.getPosition().y + TILE_SIZE/2, 0);
        camera.project(tPos);
        particles.setPosition(tPos.x,tPos.y);
        particles.start();
    }

    public int getId() {
        return id;
    }

    public void setSpawnTile(Tile t) {
        spawnTile = t;
    }

    @Override
    public Command advance() {
        return null;
    }
    
    @Override
    public void update(float delta){
        super.update(delta);
        Vector3 tPos = new Vector3(currentTile.getPosition().x + TILE_SIZE/2, currentTile.getPosition().y + TILE_SIZE/2, 0);
        camera.project(tPos);
        particles.setPosition(tPos.x,tPos.y);
        if (Locator.getEffects().size() > 0) {
            ParticleEmitter emitter = Locator.getEffects().get(Locator.getEffects().size() - 1).getEmitters().get(0);
            emitter.getScale().setHigh(1 / camera.zoom * 32);
        }
    }
    

    @Override
    public void applyEffect(MovableEntity block, Board gameBoard, Turn currentTurn, Tile tileMovedTo) {
        if (spawnTile.containsBlock()) {
            currentTurn.addCommand(new CancelCommand(currentTurn));
        } else if (!block.counterEffect(this)) {
        	Locator.getAudio().playSound(SOUND.TELEPORT.getValue());
            currentTurn.addCommand(new TeleportCommand(block, currentTile, spawnTile));
        }
    }

    @Override
    public Command retract() {
        return null;
    }
    
    @Override
	public void setInfo() {
		info="Name: Teleport Trap\nFunction: Teleports a block to the receiver";
	}

}
